package cn.wolfcode.luowowo.util;

import cn.wolfcode.luowowo.exception.LogicException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertUtil{

    /**
     * 检测用户输入的值是否为空
     * @param value
     * @param msg
     */
    public static void hasLength(String value,String msg) {
        if(value == null || "".equals(value.trim())){
            throw new LogicException(msg);
        }
    }

    /**
     * 检测两次输入的验证码是否一致
     * @param v1
     * @param v2
     * @param msg
     */
    public static void isEquals(String v1, String v2, String msg) {
        // 如果验证码为空或者两个验证码不一致,抛出异常
        if(v1== null || v2==null || !(v1.equalsIgnoreCase(v2))){
            throw new LogicException(msg);
        }
    }

    /**
     * 检测手机号格式是否正确
     * @param phone
     * @param msg
     * @param msgTo
     */
    public static void isMobileNo(String phone, String msg, String msgTo) {
        // 判断 手机号是否为空
        if("".equals(phone)){
            throw new LogicException(msg);
        }

        // 判断手机号是否正确
        Pattern compile = Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$");
        Matcher matcher = compile.matcher(phone);
        if(!(matcher.matches())){
            throw new LogicException(msgTo);
        }
    }
}
